//
//  ChooseBottomAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/26.
//

#import "ChooseBottomAlertView.h"

@interface ChooseBottomAlertView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIPickerView *pickView;
@property(nonatomic,strong)UIButton *sureBtn;
@property(nonatomic,strong)UIView *topLine;
@property(nonatomic,strong)UIView *bottomLine;

@end

@implementation ChooseBottomAlertView

-(instancetype)initWithFrame:(CGRect)frame andTitleArr:(NSArray*)titleArr{
    self = [super initWithFrame:frame];
    if(self){
        _sel_index = 0;
        self.titleArr = titleArr;
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,209)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(11, 6, 42, 42)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:cancelBtn];
    
    UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.currentAvailableWidth-42-11, 6, 42, 42)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:sureBtn];
    self.sureBtn = sureBtn;
    
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,42,self.currentAvailableWidth,126)];
    self.pickView.backgroundColor = [UIColor whiteColor];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    [self.bgView addSubview:self.pickView];
    // 添加自定义分割线
    UIView *topLine = [[UIView alloc]initWithFrame:CGRectMake(0,42+42+0.5,self.currentAvailableWidth,0.5)];
    topLine.backgroundColor = UIColorHex(0xEBEBEB);
    [self.bgView addSubview:topLine];
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,42+42*2-0.5,self.currentAvailableWidth,0.5)];
    bottomLine.backgroundColor = UIColorHex(0xEBEBEB);
    [self.bgView addSubview:bottomLine];
    self.topLine = topLine;
    self.bottomLine = bottomLine;
    
    [self pickerView:self.pickView didSelectRow:_sel_index inComponent:0];
}

-(void)refreshWidth{
    
    if(self.isShow == YES){
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight-209,self.currentAvailableWidth,209);
    }else{
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,209);
    }
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    self.sureBtn.frame = CGRectMake(self.currentAvailableWidth-42-11, 6, 42, 42);
    self.pickView.frame = CGRectMake(0,42,self.currentAvailableWidth,126);
    self.topLine.frame = CGRectMake(0,42+42+0.5,self.currentAvailableWidth,0.5);
    self.bottomLine.frame = CGRectMake(0,42+42*2-0.5,self.currentAvailableWidth,0.5);
    [self.pickView reloadAllComponents];
    
}

#pragma mark - 取消
-(void)cancelBtnClick{
    [self hidden];
}

#pragma mark - 确定
-(void)sureBtnClick{
    if(_sureBlock){
        _sureBlock(_sel_index);
    }
}


-(void)show{
    self.isShow = YES;
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight-209,self_weak_.currentAvailableWidth,209);
    } completion:^(BOOL finished) {
        [self_weak_.pickView selectRow:self_weak_.sel_index inComponent:0 animated:NO];
    }];
}

-(void)hidden{
    self.isShow = NO;
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight,self_weak_.currentAvailableWidth,209);
    } completion:^(BOOL finished) {
        [self_weak_ removeFromSuperview];
    }];
}

#pragma mark - pickerview
//设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//设置指定列包含的项数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.titleArr.count;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 42;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UIView *view  = (UILabel *)[pickerView viewForRow:row forComponent:component];
    UILabel *piketLabel = (UILabel *)[view viewWithTag:2323+row];
    piketLabel.textColor = KFontBlackColor;
    piketLabel.font = JPUIFontPingFangSCMedium(16);
    
    _sel_index = row;
}


-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    if (!view) {
        view = [[UIView alloc]init];
    }
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews){
        singleLine.backgroundColor = [UIColor clearColor];
    }

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,1,self.currentAvailableWidth,40)];
    titleLabel.textColor = KFontGrayColor;
    titleLabel.font = JPUIFontPingFangSCRegular(15);
    titleLabel.text = self.titleArr[row];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.tag = 2323+row;
    [view addSubview:titleLabel];
    
    return view;
    
}


@end
